package leetcode.editor.cn;

//给你链表的头结点 head ，请将其按 升序 排列并返回 排序后的链表 。 
//
// 
// 
//
// 
//
// 示例 1： 
//
// 
//输入：head = [4,2,1,3]
//输出：[1,2,3,4]
// 
//
// 示例 2： 
//
// 
//输入：head = [-1,5,3,4,0]
//输出：[-1,0,3,4,5]
// 
//
// 示例 3： 
//
// 
//输入：head = []
//输出：[]
// 
//
// 
//
// 提示： 
//
// 
// 链表中节点的数目在范围 [0, 5 * 10⁴] 内 
// -10⁵ <= Node.val <= 10⁵ 
// 
//
// 
//
// 进阶：你可以在 O(n log n) 时间复杂度和常数级空间复杂度下，对链表进行排序吗？ 
// Related Topics 链表 双指针 分治 排序 归并排序 👍 1724 👎 0

import java.util.*;

//Java：排序链表
public class SortList_148{
    public static void main(String[] args) {
        Solution solution = new SortList_148().new Solution();
        // TO TEST
    }
    
    //leetcode submit region begin(Prohibit modification and deletion)
/**
 * Definition for singly-linked list.
 * public class ListNode {
 *     int val;
 *     ListNode next;
 *     ListNode() {}
 *     ListNode(int val) { this.val = val; }
 *     ListNode(int val, ListNode next) { this.val = val; this.next = next; }
 * }
 */
class Solution {
    public ListNode sortList(ListNode head) {
//        System.out.println(toS(head));
        if(head==null || head.next ==null)
            return head;
        Deque<ListNode> deque = new LinkedList<>();
        ListNode node = head;
        while (node!=null){
            deque.add(new ListNode(node.val));
            node = node.next;
        }
        while (deque.size()>1){
//            System.out.println("size:"+deque.size());
            int size = deque.size();
            for (int i = 0; i < size / 2; i++) {
                ListNode head1 =deque.pop();
                ListNode head2 = deque.pop();
//                System.out.println(toS(head1) + "+" + toS(head2));
                ListNode merge = merge(head1, head2);
                deque.add(merge);
            }
        }
        return deque.pop();

    }

    public ListNode merge(ListNode head1, ListNode head2){
        ListNode head = new ListNode();
        ListNode node = head;
        ListNode node1 = head1;
        ListNode node2 = head2;
        while(node1!=null && node2!=null){
            if(node1.val < node2.val){
                node.next = node1;
                node1 = node1.next;
                node = node.next;
            }else{
                node.next = node2;
                node2 = node2.next;
                node = node.next;
            }
        }
        while (node1!=null){
            node.next = node1;
            node1 = node1.next;
            node = node.next;
        }
        while(node2!=null){
            node.next = node2;
            node2 = node2.next;
            node = node.next;
        }
        return head.next;
    }
    public String toS(ListNode node) {
        List<Integer> list = new ArrayList<>();
        while (node!=null){
            list.add(node.val);
            node = node.next;
        }
        return list.toString();
    }
}
//leetcode submit region end(Prohibit modification and deletion)
public class ListNode {
    int val;
    ListNode next;
    ListNode() {}
    ListNode(int val) { this.val = val; }
    ListNode(int val, ListNode next) { this.val = val; this.next = next; }
}
}
